<template>
  <!-- 导航栏上的阅读页面 -->
  <el-container class="m-content-display">
    <el-header><Header /></el-header>
    <el-main>
      <div class="main">
        <el-card class="left">
          <div slot="header">
            <ul class="first-tag">
              <li>
                <a href="http://localhost:9528/#/articleList" :class="this.$route.params.id ? '': 'selected' ">全部</a>
              </li>
              <li v-for="type in typeList" :key="type.id">
                <router-link :to="{name: 'ArticleList',params:{id: type.id}}">
                  <span :ref="type.name" :class="routeId === type.id ? 'selected': ''">{{ type.name }}</span>
                </router-link>
              </li>
            </ul>
          </div>
          <div class="article-list">
            <Article v-for="article in articleList" :key="article.id" :article="article" class="item" />
            <div class="paging">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-count="pages"
                @current-change="pageNumChange"
              />
            </div>
          </div>
        </el-card>
        <el-card class="right">
          <el-image src="http://rbfvta36h.hd-bkt.clouddn.com/qingsu.jpg" />
          <span class="tip">试着写下你的感受,写下你的经历,或许文字可以帮你记录帮你改善!</span>
          <div class="entrance-m">
            <a target="_blank" class="write-article" @click="writeArticle">写文章</a>
          </div>
        </el-card>
      </div>
    </el-main>
    <el-footer>
      <Footer />
    </el-footer>
  </el-container>
</template>

<script>
import Header from '../header/index.vue'
import { getTypeListAll, getArticleByTypeId } from '@/api/article'
import Article from './list/index.vue'
import Footer from '../foot/foot.vue'
export default {
  components: { Header, Article, Footer },
  data() {
    return {
      typeList: [],
      articleList: [],
      pages: 0,
      pageNum: 1,
      typeId: '',
      routeId: this.$route.params.id
    }
  },
  watch: {
    '$route'() {
      this.routeId = this.$route.params.id
      this.getArticleList()// 我的初始化方法
      console.log(this.routeId)
    }
  },
  mounted() {
    this.getTypeList()
    this.getArticleList()
  },
  methods: {
    getTypeList() {
      getTypeListAll().then(res => {
        this.typeList = res.data
      })
    },
    getArticleList() {
      getArticleByTypeId({ id: this.$route.params.id, pageIndex: this.pageNum, pageSize: 20 }).then(res => {
        this.articleList = res.data.list
        this.pages = res.data.pages
        this.pageNum = res.data.pageNum
      })
    },
    pageNumChange(value) {
      this.pageNum = value
      this.getArticleList(this.typeId)
    },
    gotoByTypeId(id) {
      this.$router.replace('/articleList/' + id)
    },
    writeArticle() {
      this.$router.replace('/publishArticle')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-header{
  padding: 0;
}
.el-main {
  padding: 10px 0 10px 0;
  position: relative;
  min-height: 100%;
  background-color: #f3f4f5;
}
.el-footer{
  padding: 0;
}
.main {
    width: 1180px;
    height: 100%;
    margin: 0 auto;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    padding-top: 20px;
    -webkit-box-align: start;
    -ms-flex-align: start;
    align-items: flex-start;
}
.left {
  width: 800px;
  border-radius: 20px;
}
.right {
    width: 360px;
    margin-left: 20px;
    border-radius: 10px;
    padding: 20px;
  border-radius: 20px;
}
.first-tag {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    padding: 9px 0 0;
    margin: 0;
    li {
      margin-right: 10px;
      margin-top: 9px;
      list-style: none;
      display: inline-block;
      font-family: PingFangSC-Regular;
      font-size: 16px;
      color: #666;
      line-height: 18px;
      text-decoration: none;
    }
}
.selected{
  color: #409EFF;
}
.paging {
  text-align: center;
  padding-top: 20px;
}
.entrance-m {
    margin-top: 30px;
    margin-left: 65px;
    a {
      display: block;
      text-decoration: none;
      background: #0b8bff;
      border-radius: 8px;
      width: 150px;
      height: 42px;
      line-height: 42px;
      color: #fff;
      font-family: PingFangSC-Regular;
      font-size: 16px;
      text-align: center;
    }
}
.tip {
  font-size: 14px;
  color: #909399;
  text-align: center;
  line-height: 1.7;
}
.m-content-display {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
</style>
